//
//  ChannelDetailViewController.m
//  kankanVideo
//
//  Created by wangbo on 6/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ChannelDetailViewController.h"
#import "SinaController.h"

@implementation ChannelDetailViewController
@synthesize lblTitle,weiboEngine,_pageurl;
//@synthesize playerViewController;
//@synthesize playerView;


-(id)init{
    self=[super init];
    videoList=[[VideoList alloc]init];
    [videoList.tableView setFrame:CGRectMake(0, 44, 320, 416)];
    videoList.delegate=self;
    //playerViewController=[[MyPlayerViewController alloc]init];
    [self.view addSubview:videoList.tableView];
    [videoList.tableView release];
    return self;
}
-(IBAction)btnquit:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)btnrefresh:(id)sender{
    [videoList loaddata:clsid needRefresh:YES];
}
-(void)dealloc{
    [lblTitle release];
    //[playerViewController release];
    [super dealloc];
}
-(void)setPageTitle:(NSString *)theTitle{
    lblTitle.text=theTitle;
}
/*-(void)myloaddata:(NSString *)theTitle withURL:(NSString *)theURL withClassid:(NSString *)clsid{
    lblTitle.text=theTitle;
    [videoList myreloaddata:theURL withClassid:clsid];
}*/

-(void)showVideoList:(NSMutableArray *)param{
    NSLog(@"showVideoList");
    lblTitle.text=[param objectAtIndex:1];
    clsid=[param objectAtIndex:0];
    [videoList loaddata:clsid needRefresh:NO];
}

-(void)playMovieWithURL:(NSString *)weburl{
    NSLog(@"playMovieWithURL:%@",weburl);
    //if(!playerViewController)playerViewController=[[MyPlayerViewController2 alloc]init];
    //[self.navigationController pushViewController:playerViewController animated:YES];
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app showAlert:@"正在加载"];
    NSString *str=[NSString stringWithFormat:@"http://interface.kankanews.com/kkapi/mobile/mobileapi.php?m=vcontent&url=%@",weburl];
    [[MyRequest shareInstance]loaddataWithURL:str forObject:self];
}
-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
    if(request.responseStatusCode==200){
        NSString *_url=[[[XMLParser parseVideoDetail:request.responseData]objectAtIndex:0]objectAtIndex:0];
        //if(!moviePlayer)moviePlayer=[[MPMoviePlayerView2 alloc]init];
        [MPMoviePlayerView2 shareInstance].deleagate=self;
        [[MPMoviePlayerView2 shareInstance] playURLVideo:_url];
        //moviePlayer.deleagate=self;
        //[moviePlayer playURLVideo:_url];
        NSLog(@"moviePlayer0_retain:%d",[MPMoviePlayerView2 shareInstance].retainCount);
        //[self presentModalViewController:moviePlayer animated:YES];
        //[self presentModalViewController:[MPMoviePlayerView2 shareInstance] animated:YES];
        [self presentMoviePlayerViewControllerAnimated:[[MPMoviePlayerView2 shareInstance] getPlayer]];
    }
}

-(void)shareVideo:(NSString *)pageurl{
    _pageurl=pageurl;
    if ([[SinaController shareInstance]isLogin]) {
        [[SinaController shareInstance]sendWithContent:_pageurl withImage:nil];
        AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
        [app showAlert:@"分享成功"];
        [self performSelector:@selector(alertover) withObject:nil afterDelay:3];
    }else{
        [[SinaController shareInstance] setDelegate:self];
        [[SinaController shareInstance] login:self];
    }
    //[[SinaController shareInstance] setDelegate:self];
    //[[SinaController shareInstance] login:self];
}

-(void)didLogin{
    [[SinaController shareInstance]sendWithContent:_pageurl withImage:nil];
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app showAlert:@"分享成功"];
    [self performSelector:@selector(alertover) withObject:nil afterDelay:3];
}

-(void)alertover{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    [app hideAlert];
}

-(void)playover{
    //[self.navigationController popViewControllerAnimated:YES];
    //[self dismissModalViewControllerAnimated:YES];
    [self dismissMoviePlayerViewControllerAnimated];
    NSLog(@"moviePlayer_retain:%d",[MPMoviePlayerView2 shareInstance].retainCount);
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //playerView=[[MPMoviePlayerView alloc]initWithFrame:self.view.frame];
    NSLog(@"========channellist_viewdidload=======");
    NSLog(@"width:%f and height:%f",self.view.frame.size.width,self.view.frame.size.height);
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //[videoList.entries release];
    //videoList.entries=nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
